package com.bmob.lover.ui.fragment.user

import android.content.Intent
import android.view.View
import androidx.fragment.app.viewModels
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet
import com.bmob.lover.R
import com.bmob.lover.data.model.enums.TYPE_FORGET_PASSWORD
import com.bmob.lover.data.model.enums.TYPE_REGISTER
import com.bmob.lover.databinding.FragmentLoginBinding
import com.bmob.lover.ui.activity.IndexActivity
import com.bmob.lover.ui.fragment.base.BaseFragment
import com.bmob.lover.viewmodel.LoginViewModel
import com.bmob.lover.viewmodel.request.LoverBindRequestViewModel
import com.theone.mvvm.core.app.ext.addFailTipsObserve
import com.theone.mvvm.core.app.ext.qmui.showGridBottomSheet
import com.theone.mvvm.ext.qmui.addLeftCloseImageBtn
import com.theone.mvvm.ext.qmui.hideLoadingDialog
import com.theone.mvvm.ext.qmui.showSuccessTipsDialog

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-04-19 13:58
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class LoginFragment : BaseFragment<LoginViewModel, FragmentLoginBinding>() {


    private val mBindVm: LoverBindRequestViewModel by viewModels()

    override fun isExitPage(): Boolean = true

    override fun initView(root: View) {
        getTopBar()?.run {
            setTitle(R.string.login)
            updateBottomDivider(0, 0, 0, 0)
            addLeftCloseImageBtn(this@LoginFragment, R.drawable.mz_comment_titlebar_ic_close_dark)
        }
    }

    override fun createObserver() {
        addFailTipsObserve(getViewModel(), mBindVm)
        getViewModel().run {
            getResponseLiveData().observe(this@LoginFragment) {
                mAppVm.setUserInfo(it)
                mBindVm.mUserId = it.objectId
                mBindVm.requestServer()
            }
        }
        mBindVm.getFinallyLiveData().observe(this) {
            hideLoadingDialog()
        }
        mBindVm.getResponseLiveData().observe(this) {
            if (it.isNotEmpty()) {
                it[0].let { bind ->
                    mAppVm.setLoverBind(bind)
                }
            }
            context?.showSuccessTipsDialog("登录成功") {
                startActivity(Intent(activity, IndexActivity::class.java))
                mActivity.finish()
            }
        }
    }

    private fun showRegisterMethodDialog() {
        QMUIBottomSheet.BottomListSheetBuilder(context).apply {
            setTitle("注册方式")
            setAddCancelBtn(true)
            setGravityCenter(true)
            setAllowDrag(false)
            setNeedRightMark(false)
            addItem("用户名")
            addItem("手机号码")
            setOnSheetItemClickListener { dialog, itemView, position, tag ->
                dialog.dismiss()
                startFragment(
                    if (position == 0) {
                        RegisterFragment.newInstance()
                    } else {
                        SendVerifyCodeFragment.newInstance(TYPE_REGISTER)
                    }
                )
            }
        }.build().show()
    }

    override fun getBindingClick() = ProxyClick()

    inner class ProxyClick {

        fun login() {
            getViewModel().requestServer()
        }

        fun forgetPassword() {
            startFragment(SendVerifyCodeFragment.newInstance(TYPE_FORGET_PASSWORD))
        }

        fun register() {
            startFragment(RegisterFragment.newInstance())
        }
    }

}